<?php
if(!defined('inside')) die('You do not have permission to execute this file directly.');

/**
 * Creates a new form
 * @param String $action The adress of the form's action property, example: blog/addpost
 * @param String $method POST or GET
 * @param boolean $multipart Whether to set the enctype to multipart, for file uploading
 */
function form_open($action, $method = 'POST', $multipart = FALSE, $id = NULL) {
	$config = AkaiKiwi::loadLib('Config');
	return '<form '.($id == NULL ? '' : ' id="'.$id.'" ').' '.($multipart ? 'enctype="multipart/form-data" ' : '').'action="'.$config->get_item('basePath').'/'.$action.'/" method="'.$method.'">';
}

function form_input($name, $id = NULL, $default = '') {
	return '<input value="'.$default.'" name="'.$name.'" id="'.($id == NULL ? $name : $id).'" type="text" />';
}

function form_password($name, $id = NULL, $default = '') {
	return '<input name="'.$name.'" id="'.($id == NULL ? $name : $id).'" value="'.$default.'" type="password" />';
}

function form_file($name, $id = NULL) {
	return '<input type="file" name="'.$name.'" id="'.($id == NULL ? $name : $id).'" />';
}

function form_hidden($name, $id = NULL, $value = '') {
	return '<input type="hidden" name="'.$name.'" id="'.($id == NULL ? $name : $id).'" value="'.$value.'" />';
}

function form_textarea($name, $id = NULL, $text = "", $params = NULL) {
	$r = '<textarea name="'.$name.'" id="'.($id == NULL ? $name : $id).'"';
	if(is_array($params)) {
		foreach($params as $k => $v) {
			$r .= ' '.$k.'="'.$v.'" ';
		}
	} else {
	    $r .= ' cols="40" rows="20" ';
	}
	$r .= '>'.$text.'</textarea>';
	return $r;
}

function form_submit($name, $id = NULL) {
	return '<input value="'.$name.'" name="Submit" id="'.($id == NULL ? $name : $id).'" type="submit" />';
}

function form_close() {
	return '</form>';
}

function form_dropdown($name, $id = NULL, $options, $selected = NULL, $params = array()) {
	$r = '<select name="'.$name.'" id="'.($id == NULL ? $name : $id).'"';
	foreach($params as $k => $v) {
		$r .= ' '.$k.'="'.$v.'"';
	}
	$r .= '>';
	foreach($options as $k => $v) {
		$r .= '<option value="'.$k.'"'.($selected == $k ? ' selected="selected"' : '').'>'.$v.'</option>';
	}
	$r .= '</select>';
	return $r;
}

function form_date($name, $id = NULL, $order = '{d}{m}{y}') {
	$years = array();
	for($i = date('Y'); $i >= 1900; $i--) {
		$years[$i] = $i;
	}
	
	$months = array();
	for($i = 1; $i <= 12; $i++) {
		$months[$i] = $i;
	}

	$days = array();
	for($i = 1; $i <= 31; $i++) {
		$days[$i] = $i;
	}
	
	$y = form_dropdown($name . '_year', ($id == NULL ? $name : $id) . '_year', $years);
	$m = form_dropdown($name . '_month', ($id == NULL ? $name : $id) . '_months', $months);
	$d = form_dropdown($name . '_day', ($id == NULL ? $name : $id) . '_day', $days);

	$r = str_replace(array('{y}', '{m}', '{d}'), array($y, $m, $d), $order);

	return $r;
}

function form_checkbox($name, $id = NULL, $default = '', $checked = FALSE) {
	return '<input type="checkbox" name="'.$name.'" id="'.($id == NULL ? $name : $id).'" value="'.$default.'" '.($checked ? 'checked="checked" ' : '').'/>';
}

function form_radio($name, $id = NULL, $default = '', $checked = FALSE) {
	return '<input type="radio" name="'.$name.'" id="'.($id == NULL ? $name : $id).'" value="'.$default.'" '.($checked ? 'checked="checked" ' : '').'/>';
}
